package customer.zspt.cn.customer.Widget.chat;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.components.chat.FriendChat;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.utils.MediaPlayUtils;
import customer.zspt.cn.customer.utils.NetUtils;
import customer.zspt.cn.customer.utils.SPUtils;
import customer.zspt.cn.customer.utils.TimestampToBj;
import customer.zspt.cn.customer.utils.UnloadFileUtils;

/**
 * Created by Administrator on 2018/8/8 0008.
 */

public class ChatVocieMsgItem extends LinearLayout implements View.OnClickListener {
    //录音是否在播放
    public boolean IsPlay = false;
    private String token;
    private UnloadFileUtils unloadFileUtils;
    /**
     * 播放音频
     */
    @ViewInject(R.id.playVoice)
    private LinearLayout playVoice;
    @ViewInject(R.id.play)
    private ImageView play;
    @ViewInject(R.id.httpAudio)
    private TextView httpAudio;
    @ViewInject(R.id.header_portrait)
    private ImageView header_portrait;
    @ViewInject(R.id.msg_time)
    private TextView msg_time;
    @ViewInject(R.id.voice_time)
    private TextView voice_time;
    /*** 上传录音提示条*/
    @ViewInject(R.id.loadingPr)
    private ProgressBar loadingPr;
    private BitmapUtils utils;
    private boolean IsUrl = false;
    //录音本地路径
    private String audioEnPath = null;
    @ViewInject(R.id.fail_tip)
    private TextView fail_tip;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0) {
                loadingPr.setVisibility(GONE);
            } else if (msg.arg1 == -1) {
                showFailTip();
            }
        }
    };
    private AnimationDrawable animationDrawable;

    public ChatVocieMsgItem(Context context, boolean isLeft) {
        this(context, null, isLeft);
    }

    public ChatVocieMsgItem(Context context, @Nullable AttributeSet attrs, boolean isLeft) {
        this(context, attrs, 0, isLeft);
    }

    public ChatVocieMsgItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr, boolean isLeft) {
        super(context, attrs, defStyleAttr);
        utils = new BitmapUtils(context);
        if(isLeft){
            View.inflate(context, R.layout.voicefrommsg, this);
        }else{
            View.inflate(context, R.layout.voiceminemsg, this);
        }
        ViewUtils.inject(this);
        playVoice.setOnClickListener(this);
        animationDrawable = (AnimationDrawable) play.getBackground();
    }

    public ChatVocieMsgItem(Context context, String path, final Activity activity, final long time, boolean isLeft) {
        super(context);
        utils = new BitmapUtils(context);
        if(isLeft){
            View.inflate(context, R.layout.voicefrommsg, this);
        }else{
            View.inflate(context, R.layout.voiceminemsg, this);
        }
        ViewUtils.inject(this);
        audioEnPath = path;
        loadingPr.setVisibility(VISIBLE);
        /**设置录音时间*/
        audioTime(time + "");
        unloadFileUtils = new UnloadFileUtils();
        token = SPUtils.getString(context, "token", "");
        if (NetUtils.getNetWorkState(context) == NetUtils.NOTNET) {
            showFailTip();
        } else {
            unloadFileUtils.onLoadFile(path, "audio/video", constant.uploadVoice, token);
        }
        unloadFileUtils.getResultLintener(new UnloadFileUtils.OnResultLintener() {
            @Override
            public void OnSuccess(String result) {
                Message message = new Message();
                message.arg1 = 0;
                mHandler.sendMessage(message);
                FriendChat friendChat = (FriendChat) activity;
                friendChat.pushFileData(result, "voice", time);
            }

            @Override
            public void OnError() {

            }

            @Override
            public void OnFail() {
                Message message = new Message();
                message.arg1 = -1;
                mHandler.sendMessage(message);
            }
        });
        playVoice.setOnClickListener(this);
        fail_tip.setOnClickListener(this);
        animationDrawable = (AnimationDrawable) play.getBackground();
    }

    private void showFailTip() {
        loadingPr.setVisibility(GONE);
        fail_tip.setVisibility(VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.playVoice:
                if (NetUtils.getNetWorkState(getContext()) == NetUtils.NOTNET) {
                    if (audioEnPath != null) {
                        MediaPlayUtils.getInstance().setPreparedListener(this);
                        MediaPlayUtils.getInstance().playLoad(audioEnPath);
                    } else {
                        Toast.makeText(getContext(), "无网络", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (IsPlay) {
                        IsPlay = false;
                        MediaPlayUtils.getInstance().stop();
                    } else {
                        IsPlay = true;
                        //播放本地音频
                        if (audioEnPath != null) {
                            MediaPlayUtils.getInstance().setPreparedListener(this);
                            MediaPlayUtils.getInstance().playLoad(audioEnPath);
                        } else {
                            MediaPlayUtils.getInstance().setPreparedListener(this);
                            MediaPlayUtils.getInstance().playUrl(httpAudio.getText().toString());
                        }
                    }
                }
                break;
            case R.id.fail_tip:
                fail_tip.setVisibility(GONE);
                loadingPr.setVisibility(VISIBLE);
                unloadFileUtils.onLoadFile(audioEnPath, "audio/video", constant.uploadVoice, token);
                break;
        }
    }

    /**
     * 是否播放动画
     */
    public void IsPlayAnimation(boolean isPlay) {
        if (isPlay) {
            if (!animationDrawable.isRunning()) {
                animationDrawable.start();
            }
        } else {
            if (animationDrawable.isRunning()) {
                animationDrawable.selectDrawable(0);
                animationDrawable.stop();
            }
        }
    }

    /**
     * 设置网站
     */
    public void setHttpAudio(String url) {
        IsUrl = true;
        httpAudio.setText(url);
    }

    /**
     * 设置头像
     */
    public void setAvater(String url) {
        utils.display(header_portrait, url);
    }

    /**
     * 音频时间
     */
    public void audioTime(String time) {
        voice_time.setText(zhTime(Integer.parseInt(time)));
    }

    public String zhTime(long time) {
        String timeStr = "";
        long m = time / 1000 / 60;
        long s = time / 1000 % 60;
        if (m == 0) {
            timeStr = s + "''";
        } else {
            timeStr = m + "'" + s + "''";
        }
        return timeStr;
    }

    /**
     * 设置时间
     */
    public void setTime(long time, long ChatTime) {
        if (time - ChatTime > 3 * 60 * 1000) {
            msg_time.setText(TimestampToBj.getBeijingTime(time));
        } else {
            msg_time.setVisibility(View.GONE);
        }
    }

}
